package com.example.design.mediator;

import java.util.HashMap;
import java.util.Map;

/**
 * 具体中介者角色
 * @author hej
 * @date 2022/11/4 17:27
 */
public class ConcreteMediator implements Mediator {
    /**
     * 用于存放所有的同事对象
     */
    private Map<String,Colleague> colleagueMap;

    private Map<String,String> interMap;

    public ConcreteMediator() {
        this.colleagueMap = new HashMap<>();
        this.interMap = new HashMap<>();
    }


    @Override
    public void register(String colleagueName, Colleague colleage) {
        colleagueMap.put(colleagueName,colleage);

        if(colleage instanceof Alarm){
            interMap.put("alarm",colleagueName);
        }else if(colleage instanceof CoffeeMachine){
            interMap.put("coffeeMachine",colleagueName);
        }else if (colleage instanceof TV){
            interMap.put("tv",colleagueName);
        }else if (colleage instanceof Curtains){
            interMap.put("curtains",colleagueName);
        }
    }

    @Override
    public void getMessage(int state, String colleagueName) {
        if (colleagueMap.get(colleagueName) instanceof Alarm){
            if(state == 0){
                ((CoffeeMachine)colleagueMap.get(interMap.get("coffeeMachine"))).startCoffee();
                ((TV)colleagueMap.get(interMap.get("tv"))).startTv();
            }else if(state == 1){
                ((TV)colleagueMap.get(interMap.get("tv"))).stopTv();
            }
        }else if(colleagueMap.get(colleagueName) instanceof CoffeeMachine){
            ((Curtains)colleagueMap.get(interMap.get("curtains"))).upCurtains();
        }
    }

    @Override
    public void sendMessage() {

    }
}
